import type {
  MultiDomainProperties,
  DomainProperties,
  PropertyQueryParams,
  PropertyUpdateParams,
  PropertyResponse,
  DomainPropertyResponse,
  FieldTemplate,
  DomainTemplate,
} from './types';
import { PropertyDomain } from './types';
import { mockRequestClient as defHttp } from '#/api/request';

enum Api {
  // 多领域属性管理
  GetMultiDomainProperties = '/resource-center/multi-domain-properties',
  UpdateDomainProperties = '/resource-center/domain-properties',
  SaveMultiDomainProperties = '/resource-center/multi-domain-properties',

  // 字段模板管理
  GetFieldTemplates = '/resource-center/field-templates',
  GetDomainTemplates = '/resource-center/domain-templates',
  ApplyDomainTemplate = '/resource-center/apply-domain-template',

  // 配置导入导出
  ExportDomainConfig = '/resource-center/export-domain-config',
  ImportDomainConfig = '/resource-center/import-domain-config',
}

/**
 * 获取资源的多领域属性
 */
export function getMultiDomainProperties(params: PropertyQueryParams) {
  return defHttp.get<PropertyResponse>(Api.GetMultiDomainProperties, {
    params,
  });
}

/**
 * 更新指定领域的属性
 */
export function updateDomainProperties(params: PropertyUpdateParams) {
  return defHttp.put<DomainPropertyResponse>(
    Api.UpdateDomainProperties,
    params,
  );
}

/**
 * 保存完整的多领域属性配置
 */
export function saveMultiDomainProperties(
  resourceId: string,
  properties: MultiDomainProperties,
) {
  return defHttp.post<PropertyResponse>(Api.SaveMultiDomainProperties, {
    resourceId,
    properties,
  });
}

/**
 * 获取字段模板列表
 */
export function getFieldTemplates(domain?: PropertyDomain) {
  return defHttp.get<{
    success: boolean;
    data: FieldTemplate[];
    message?: string;
  }>(Api.GetFieldTemplates, { params: { domain } });
}

/**
 * 获取领域模板列表
 */
export function getDomainTemplates(domain?: PropertyDomain) {
  return defHttp.get<{
    success: boolean;
    data: DomainTemplate[];
    message?: string;
  }>(Api.GetDomainTemplates, { params: { domain } });
}

/**
 * 应用领域模板
 */
export function applyDomainTemplate(
  resourceId: string,
  templateId: string,
  domain: PropertyDomain,
) {
  return defHttp.post<PropertyResponse>({
    url: Api.ApplyDomainTemplate,
    data: {
      resourceId,
      templateId,
      domain,
    },
  });
}

/**
 * 导出领域配置
 */
export function exportDomainConfig(
  resourceId: string,
  domain?: PropertyDomain,
) {
  return defHttp.get({
    url: Api.ExportDomainConfig,
    params: { resourceId, domain },
    responseType: 'blob',
  });
}

/**
 * 导入领域配置
 */
export function importDomainConfig(resourceId: string, configFile: File) {
  const formData = new FormData();
  formData.append('resourceId', resourceId);
  formData.append('configFile', configFile);

  return defHttp.post<PropertyResponse>({
    url: Api.ImportDomainConfig,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}

/**
 * 获取默认的多领域属性配置
 */
export function getDefaultMultiDomainProperties(): MultiDomainProperties {
  return {
    domains: {
      BUSINESS: {
        schema: [],
        data: {},
        config: {
          key: PropertyDomain.BUSINESS,
          name: '业务领域',
          description: '核心业务属性，如价格、房型、设施等',
          icon: 'ant-design:shop-outlined',
          color: '#1890ff',
          visible: true,
          editable: true,
          required: false,
          order: 0,
        },
      },
      OPERATION: {
        schema: [],
        data: {},
        config: {
          key: PropertyDomain.OPERATION,
          name: '运营领域',
          description: '运营相关属性，如渠道、库存、合作方等',
          icon: 'ant-design:setting-outlined',
          color: '#52c41a',
          visible: true,
          editable: true,
          required: false,
          order: 1,
        },
      },
      TECHNICAL: {
        schema: [],
        data: {},
        config: {
          key: PropertyDomain.TECHNICAL,
          name: '技术领域',
          description: '技术相关属性，如版本、API、环境等',
          icon: 'ant-design:code-outlined',
          color: '#722ed1',
          visible: true,
          editable: true,
          required: false,
          order: 2,
        },
      },
      COMPLIANCE: {
        schema: [],
        data: {},
        config: {
          key: PropertyDomain.COMPLIANCE,
          name: '合规风控',
          description: '合规和风控属性，如资质、状态、隐私等',
          icon: 'ant-design:safety-outlined',
          color: '#fa8c16',
          visible: true,
          editable: true,
          required: false,
          order: 3,
        },
      },
    },
    metadata: {
      version: '1.0.0',
      lastModified: new Date().toISOString(),
      modifiedBy: '',
      domainCount: 4,
      totalFields: 0,
    },
  };
}
